package lab3_2;

import org.junit.Test;
import static org.junit.Assert.*;

public class BinaryOperationTest {
    
    @Test
    public void testAdditionOperation() {
        AdditionOperation op = new AdditionOperation(5, 3);
        assertEquals(5, op.getLeftOperand());
        assertEquals(3, op.getRightOperand());
        assertEquals('+', op.getOperator());
        assertEquals(8, op.getValue());
        assertTrue(op.checkingCalculation(5, 3));
        assertFalse(op.checkingCalculation(100, 1)); // 101 > 100
    }
    
    @Test
    public void testSubstractOperation() {
        SubstractOperation op = new SubstractOperation(10, 4);
        assertEquals(10, op.getLeftOperand());
        assertEquals(4, op.getRightOperand());
        assertEquals('-', op.getOperator());
        assertEquals(6, op.getValue());
        assertTrue(op.checkingCalculation(10, 4));
        assertFalse(op.checkingCalculation(5, 10)); // -5 < 0
    }
    
    @Test
    public void testGenerateOperation() {
        BinaryOperation addOp = BinaryOperation.generateOperation('+');
        BinaryOperation subOp = BinaryOperation.generateOperation('-');
        
        assertTrue(addOp instanceof AdditionOperation);
        assertTrue(subOp instanceof SubstractOperation);
        assertEquals('+', addOp.getOperator());
        assertEquals('-', subOp.getOperator());
    }
    
    @Test
    public void testExerciseInheritance() {
        Exercise exercise = new Exercise();
        assertTrue(exercise.isEmpty());
        
        exercise.generateAdditionExercise(5);
        assertEquals(5, exercise.size());
        assertTrue(exercise.get(0) instanceof BinaryOperation);
    }
    
    @Test
    public void testPolymorphism() {
        BinaryOperation op1 = new AdditionOperation(8, 2);
        BinaryOperation op2 = new SubstractOperation(8, 2);
        
        // 多态调用
        assertEquals(10, op1.calculate(8, 2));
        assertEquals(6, op2.calculate(8, 2));
        
        assertTrue(op1.checkingCalculation(8, 2));
        assertTrue(op2.checkingCalculation(8, 2));
    }
}